/*
 * MainPanel.java
 *
 * Created on May 23, 2006, 2:01 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author Timoz
 */
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;

public class MainPanel extends JPanel implements Serializable {
    JPanel panel;
    int scrollHorizontalValue=0;
    int scrollVerticalValue=0;
    protected javax.swing.JPanel drawPanel;
    protected javax.swing.JScrollBar scrollHorizontal;
    protected javax.swing.JScrollBar scrollVertical;
    OutputPanel outputPanel;
    
    /** Creates new form MainPanel */
    public MainPanel(JPanel panel2) {
        this.panel=panel2;
        scrollHorizontal = new javax.swing.JScrollBar();
        scrollVertical = new javax.swing.JScrollBar();
        drawPanel = new javax.swing.JPanel();
        
        setLayout(new java.awt.BorderLayout());
        
        scrollHorizontal.setMaximum(0);
        scrollHorizontal.setOrientation(javax.swing.JScrollBar.HORIZONTAL);
        scrollHorizontal.setAutoscrolls(true);
        scrollHorizontal.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
            public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {
                scrollHorizontalAdjustmentValueChanged(evt);
            }
        });
        
        add(scrollHorizontal, java.awt.BorderLayout.SOUTH);
        
        scrollVertical.setAutoscrolls(true);
        scrollVertical.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
            public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {
                scrollVerticalAdjustmentValueChanged(evt);
            }
        });
        
        add(scrollVertical, java.awt.BorderLayout.EAST);
        
        drawPanel.setAutoscrolls(true);
        drawPanel.setLayout(null);
        add(drawPanel, java.awt.BorderLayout.CENTER);
        scrollVertical.setVisible(false);
        scrollHorizontal.setVisible(false);
        panel.addMouseWheelListener(new MouseWheelListener() {
            public void mouseWheelMoved(MouseWheelEvent e) {
                int clicks=e.getWheelRotation();
                clicks *= 10;
                scrollVertical.setValue(scrollVerticalValue+clicks);
                scrollVerticalValue=scrollVertical.getValue();
            }
        });
    }
    
    private void scrollVerticalAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {
        if (scrollVertical.getValue()>scrollVerticalValue) {
            int valueDecreased=scrollVertical.getValue()-scrollVerticalValue;
            panel.setLocation(panel.getX(),panel.getY()-valueDecreased);
            repaint();
        } else {
            int valueIncreased=scrollVerticalValue-scrollVertical.getValue();
            panel.setLocation(panel.getX(),panel.getY()+valueIncreased);
            repaint();
        }
        scrollVerticalValue=scrollVertical.getValue();
    }
    
    private void scrollHorizontalAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {
        if (scrollHorizontal.getValue()>scrollHorizontalValue) {
            int valueDecreased=scrollHorizontal.getValue()-scrollHorizontalValue;
            panel.setLocation(panel.getX()-valueDecreased,panel.getY());
            repaint();
        } else {
            int valueIncreased=scrollHorizontalValue-scrollHorizontal.getValue();
            panel.setLocation(panel.getX()+valueIncreased,panel.getY());
            repaint();
        }
        scrollHorizontalValue=scrollHorizontal.getValue();
    }
    
    public void insertRecordNavigationMenu(JPanel recordPanel) {
        outputPanel=(OutputPanel)recordPanel;
        remove(scrollHorizontal);
        JPanel southPanel = new JPanel();
        southPanel.setLayout(new BorderLayout());
        southPanel.add(scrollHorizontal,BorderLayout.NORTH);
        southPanel.add(recordPanel,BorderLayout.CENTER);
        add(southPanel, BorderLayout.SOUTH);
    }
}
